package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.tsz.afinal.FinalDb;
import net.tsz.afinal.annotation.view.CodeNote;

import java.util.ArrayList;
import java.util.List;

import liuliu.custom.control.dialog.LLDialog;
import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.control.add.police.person.IRegPersonView;
import liuliu.throughwaring.control.add.police.person.RegPersonListener;
import liuliu.custom.model.CodeModel;
import liuliu.throughwaring.model.CompanyModel;
import liuliu.throughwaring.model.PersonModel;
import liuliu.throughwaring.view.Util;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 人员详细信息
 * Created by liuliu on 2015/11/18   18:24
 *
 * @author 柳伟杰
 * @Email 1031066280@qq.com
 */
public class PersonDetailActivity extends BaseActivity {
    public static PersonDetailActivity mIntails;
    String uid = "";
    @CodeNote(id = R.id.person_img_iv)
    ImageView person_img_iv;
    @CodeNote(id = R.id.person_name_tv)
    TextView person_name_tv;
    @CodeNote(id = R.id.person_sex_tv)
    TextView person_sex_tv;
    @CodeNote(id = R.id.person_status_tv)
    TextView person_status_tv;
    @CodeNote(id = R.id.person_idcard_tv)
    TextView person_idcard_tv;
    @CodeNote(id = R.id.person_nation_tv)
    TextView person_nation_tv;
    @CodeNote(id = R.id.person_birthday_tv)
    TextView person_birthday_tv;
    @CodeNote(id = R.id.person_tel_tv)
    TextView person_tel_tv;
    @CodeNote(id = R.id.person_real_address_tv)
    TextView person_real_address_tv;
    @CodeNote(id = R.id.person_unit_name_tv)
    TextView person_unit_name_tv;
    @CodeNote(id = R.id.person_job_tv)
    TextView person_job_tv;
    @CodeNote(id = R.id.person_entry_time_tv)
    TextView person_entry_time_tv;
    @CodeNote(id = R.id.person_reg_time_tv)
    TextView person_reg_time_tv;
    @CodeNote(id = R.id.person_remark_tv)
    TextView person_remark_tv;
    @CodeNote(id = R.id.person_detail_title)
    TToolbar person_detail_title;
    @CodeNote(id = R.id.zhiwu_detail_ll)
    LinearLayout zhiwu_detail_ll;//职务信息
    @CodeNote(id = R.id.check_reason_ll)
    LinearLayout check_reason_ll;//盘查原因
    @CodeNote(id = R.id.zhiwu_ll)
    LinearLayout zhiwu_ll;
    @CodeNote(id = R.id.zulin_yincang_ll)
    LinearLayout zulin_yincang_ll;
    @CodeNote(id = R.id.dengji_time_ll)
    LinearLayout dengji_time_ll;
    @CodeNote(id = R.id.cheliang_message_ll)
    LinearLayout cheliang_message_ll;//选择的是机动车时显示的内容
    @CodeNote(id = R.id.jiaotong_gongju_ll)
    LinearLayout jiaotong_gongju_ll;
    List<PersonModel> list = null;
    @CodeNote(id = R.id.detail_person_guanlian, click = "onClick")
    LinearLayout lguanlian;
    @CodeNote(id = R.id.guanlian_count)
    TextView text_guanlian;
    @CodeNote(id = R.id.chepai_id_tv)
    TextView chepai_id_tv;//车牌信息
    @CodeNote(id = R.id.chejia_id_tv)
    TextView chejia_id_tv;//车架信息
    @CodeNote(id = R.id.shibie_id_tv)
    TextView shibie_id_tv;//车辆识别码
    @CodeNote(id = R.id.pinpai_type_tv)
    TextView pinpai_type_tv;//车品牌类型
    @CodeNote(id = R.id.pancha_address_tv)
    TextView pancha_address_tv;//盘查地址
    @CodeNote(id = R.id.check_reason_tv)
    TextView check_reason_tv;//盘查原因
    @CodeNote(id = R.id.jiaotong_gongju_tv)
    TextView jiaotong_gongju_tv;//交通工具
    @CodeNote(id = R.id.person_status_ll)
    LinearLayout person_status_ll;
    @CodeNote(id = R.id.ll_pancha)
    LinearLayout ll_pancha;
    @CodeNote(id = R.id.person_real_address_tv_pancha)
    TextView txtpancha;
    @CodeNote(id = R.id.ruzhu_time_tv)
    TextView ruzhu_time_tv;//入职时间
    @CodeNote(id = R.id.lizhi_ll)
    LinearLayout lizhi_ll;
    @CodeNote(id = R.id.lizhi_time_tv)
    TextView lizhi_time_tv;//离职，离店
    @CodeNote(id = R.id.person_checkout_time_tv)
    TextView person_checkout_time_tv;//离职离店日期
    @CodeNote(id = R.id.tel_btn, click = "onClick")
    Button tel_btn;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_person_detail);
        mIntails = this;
    }

    @Override
    public void initEvents() {
        uid = getIntent().getStringExtra("personid");
        person_detail_title.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                mIntails.finish();
            }
        });
        Observable.create(new Observable.OnSubscribe<PersonModel>() {
            @Override
            public void call(Subscriber<? super PersonModel> subscriber) {
                List<PersonModel> list = finalDb.findAllByWhere(PersonModel.class, "PersonId='" + uid + "'");
                if (list.size() > 0) {
                    PersonModel model = list.get(0);
                    subscriber.onNext(model);
                }
            }
        }).subscribe(new Subscriber<PersonModel>() {
            @Override
            public void onNext(PersonModel personModel) {
                model = personModel;
                switch (model.getPersonComapnyType()) {
                    case "Company"://从业人员
                        check_reason_ll.setVisibility(View.GONE);
                        break;
                    case "Lease"://租赁人员
                        ruzhu_time_tv.setText("入住时间");
                        person_status_ll.setVisibility(View.GONE);
                        zhiwu_ll.setVisibility(View.GONE);
                        dengji_time_ll.setVisibility(View.GONE);
                        zulin_yincang_ll.setVisibility(View.GONE);
                        check_reason_ll.setVisibility(View.GONE);
                        zhiwu_detail_ll.setVisibility(View.VISIBLE);
                        break;
                    case "Free"://流动人员
                        person_status_ll.setVisibility(View.GONE);
                        zhiwu_detail_ll.setVisibility(View.GONE);
                        jiaotong_gongju_ll.setVisibility(View.VISIBLE);
                        ll_pancha.setVisibility(View.VISIBLE);
                        txtpancha.setText(Util.checkEmpty(personModel.getCheckAddress(), "河北省保定市新市区茂业中心"));
                        if (("Drive").equals(personModel.getTrafficType())) {
                            cheliang_message_ll.setVisibility(View.VISIBLE);
                            jiaotong_gongju_tv.setText("机动车");
                            chepai_id_tv.setText(personModel.getVehiclePlateNumbers());
                            chejia_id_tv.setText(personModel.getVehicleFrameNumber());
                            shibie_id_tv.setText(personModel.getVehicleIdentificationNumber());
                            pinpai_type_tv.setText(personModel.getVehicleBrandNumber());
                        } else if (("Bike").equals(personModel.getTrafficType())) {
                            jiaotong_gongju_tv.setText("自行车");
                        } else {
                            jiaotong_gongju_tv.setText("其他");
                        }
                        break;
                }
                person_name_tv.setText(personModel.getPersonName());
                String sex = personModel.getPersonSex();
                if (sex.equals("1")) {
                    person_sex_tv.setText("男");
                } else if (sex.equals("2")) {
                    person_sex_tv.setText("女");
                } else {
                    person_sex_tv.setText("未知");
                }
                String status = personModel.getPersonStatus();
                if (status.equals("OnJob")) {
                    person_status_tv.setText("在职");
                } else if (status.equals("LeaveJob")) {
                    person_status_tv.setText("离职");
                } else {
                    person_status_tv.setText("未知");
                }
                person_idcard_tv.setText(Util.checkEmpty(personModel.getPersonCardId(), "未知"));
                getNativeName(personModel.getPersonNative());
                person_birthday_tv.setText(Util.checkEmpty(personModel.getPersonBirthday(), "未知"));
                person_tel_tv.setText(Util.checkEmpty(personModel.getPersonPhoneNumber()));
                person_real_address_tv.setText(Util.checkEmpty(personModel.getPersonAddress(), "未知"));
                person_unit_name_tv.setText(Util.checkEmpty(personModel.getPersonComapnyName(), "未知"));
                person_job_tv.setText(Util.checkEmpty(personModel.getOffice(), "未知"));
                if (personModel.getPersonComapnyType().equals("Lease")) {
                    ruzhu_time_tv.setText("入住日期");
                    lizhi_time_tv.setText("退租日期");
                }
                check_reason_tv.setText(Util.checkEmpty(personModel.getCheckReason(), "无"));
                pancha_address_tv.setText(Util.checkEmpty(personModel.getPersonAddress(), "无"));
                String url = personModel.getPersonImgUrl();
                if (url != "" && url != null) {
                    person_img_iv.setImageBitmap(Utils.getLoacalBitmap(url));
                } else {
                    person_img_iv.setImageResource(R.mipmap.center_picture);
                }
                if (!personModel.getCheckInTime().equals("")) {
                    if (personModel.getCheckInTime().length() == 19) {
                        person_entry_time_tv.setText(Utils.loadTime(personModel.getCheckInTime()));
                    }
                }
                if (!("").equals(personModel.getCheckOutTime()) && personModel.getCheckOutTime() != null) {
                    lizhi_ll.setVisibility(View.VISIBLE);
                    person_checkout_time_tv.setText(Utils.loadTime(personModel.getCheckOutTime()));
                }
            }

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }
        });

        //按企业分类
        list = finalDb.findAllByWhere(PersonModel.class, "PersonCardId='" + model.getPersonCardId() + "'");
        List<CompanyModel> guList = new ArrayList<CompanyModel>();
        for (PersonModel p : list) {
            List<CompanyModel> clist = finalDb.findAllByWhere(CompanyModel.class, "CompanyId='" + p.getPersonCompanyId() + "'");
            if (clist.size() > 0) {
                Boolean isY = true;
                for (CompanyModel c : guList) {
                    if (c.getCompanyId().equals(clist.get(0).getCompanyId())) {
                        isY = false;
                        break;
                    }
                }
                if (isY) {
                    guList.add(clist.get(0));
                }

            }
        }
        text_guanlian.setText(guList.size() + "");

    }

    private void getNativeName(final String nativeid) {
        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                FinalDb mDB = FinalDb.create(mIntails);
                List<CodeModel> list = mDB.findAllByWhere(CodeModel.class, "Name='" + nativeid + "'");
                if (list.size() > 0) {
                    subscriber.onNext(list.get(0).getValue());
                } else {
                    subscriber.onNext("");
                }
            }
        }).subscribeOn(Schedulers.io()) // 指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread()) // 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<String>() {
                    @Override
                    public void onNext(String s) {
                        if (s.equals("")) {
                            s = "未知";
                        }
                        person_nation_tv.setText(s);
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }
                });
    }

    private PersonModel model;

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.detail_person_guanlian:
                //跳到关联页面
                mUtils.IntentPost(SearchCompanyListActivity.class, new Utils.putListener() {
                    @Override
                    public void put(Intent intent) {
                        intent.putExtra("CardId", model.getPersonCardId());
                    }
                });
                break;
            case R.id.tel_btn:
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + person_tel_tv.getText()));
                startActivity(intent);
                break;
        }
    }
}
